package com.mine.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mine.common.utils.IDUtils;
import com.mine.common.utils.Page;
import com.mine.dao.GoodsMapper;
import com.mine.dao.MineGoodsCategoryMapper;
import com.mine.dao.MineGoodsMapper;
import com.mine.pojo.GoodsDataInfo;
import com.mine.pojo.GoodsQueryVo1;
import com.mine.pojo.MineGoods;
import com.mine.pojo.MineGoodsCategory;
import com.mine.pojo.MineGoodsCategoryExample;
import com.mine.pojo.MineGoodsExample;
import com.mine.pojo.MineGoodsExample.Criteria;
import com.mine.pojo.MineUser;
import com.mine.result.CodeMsg;
import com.mine.result.Result;
import com.mine.vo.GoodsQueryVo;

@Service
public class GoodsServiceImpl implements GoodsService {

	@Autowired
	MineGoodsCategoryMapper mineGoodsCategoryMapper;
	@Autowired
	MineGoodsMapper goodsMapper;
	@Autowired
	UserService userService;
	
	@Autowired
	GoodsMapper gMapper;
	/**
	 * 添加新商品
	 * @param token
	 * @param goods
	 * @return
	 */
	@Override
	public Result addGoods(String token, MineGoods goods) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		//先查询该用户是否已经存在了店铺
		if (user.getShopId()==null) {
			return Result.error("未创建店铺，添加失败！");
		}
		//查询该分类是否存在
		MineGoodsCategory category = mineGoodsCategoryMapper.selectByPrimaryKey(goods.getCategoryId());
		if (category==null) {
			return Result.error("分类不存在，添加失败！");
		}
		//查询该分类是否是该用户创建的
		if (!category.getUserId().equals(user.getId())) {
			return Result.error("分类不存在，添加失败！");
		}
		goods.setId(IDUtils.genItemId());
		goods.setCreateTime(new Date());
		goods.setUpdateTime(new Date());
		goods.setUserId(user.getId());
		goods.setShopId(user.getShopId());
		int insertSelective = goodsMapper.insertSelective(goods);
		if (insertSelective>0) {
			return Result.ok();
		}
		return Result.error("添加失败！");
	}
	/**
	 * 删除商品
	 * @param token
	 * @param goods
	 * @return
	 */
	@Override
	public Result deleteGoods(String token, MineGoods goods) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		//先查询该用户是否已经存在了店铺
		if (user.getShopId()==null) {
			return Result.error("未创建店铺，删除失败！");
		}
		int deleteByPrimaryKey = goodsMapper.deleteByPrimaryKey(goods.getId());
		if (deleteByPrimaryKey>0) {
			return Result.ok();
		}
		return Result.error("删除失败！");
	}
	/**
	 * 更新某一个商品
	 * @param token
	 * @param goods
	 * @return
	 */
	@Override
	public Result updateGoods(String token, MineGoods goods) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		//先查询该用户是否已经存在了店铺
		if (user.getShopId()==null) {
			return Result.error("未创建店铺，修改失败！");
		}
		goods.setUpdateTime(new Date());
		int keySelective = goodsMapper.updateByPrimaryKeySelective(goods);
		if (keySelective>0) {
			return Result.ok();
		}
		return Result.error("更新失败！");
	}
	/**
	 * 获取商品信息
	 * @param token
	 * @param goods
	 * @return
	 */
	@Override
	public Result getGoodsInfo(String token, MineGoods goods) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		//先查询该用户是否已经存在了店铺
		if (user.getShopId()==null) {
			return Result.error("未创建店铺，查询失败！");
		}
		MineGoods mineGoods = goodsMapper.selectByPrimaryKey(goods.getId());
		if (mineGoods!=null) {
			return Result.success(mineGoods);
		}
		return Result.error("查找失败！");
	}
	/**
	 * 添加商品分类
	 */
	@Override
	public Result addGoodsCategory(String token, MineGoodsCategory goodsCategory) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		//先查询该用户是否已经存在了店铺
		if (user.getShopId()==null) {
			return Result.error("未创建店铺，添加失败！");
		}
		goodsCategory.setUserId(user.getId());
		goodsCategory.setShopId(user.getShopId());
		goodsCategory.setCreateTime(new Date());
		goodsCategory.setUpdateTime(new Date());
		int insertSelective = mineGoodsCategoryMapper.insertSelective(goodsCategory);
		if (insertSelective>0) {
			return Result.ok();
		}
		return Result.error("添加失败！");
	}
	/**
	 * 更新商品分类
	 * @param token
	 * @param goodsCategory
	 * @return
	 */
	@Override
	public Result updateGoodsCategory(String token, MineGoodsCategory goodsCategory) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		//先查询该用户是否已经存在了店铺
		if (user.getShopId()==null) {
			return Result.error("未创建店铺，更新失败！");
		}
		goodsCategory.setUpdateTime(new Date());
		int insertSelective = mineGoodsCategoryMapper.updateByPrimaryKeySelective(goodsCategory);
		if (insertSelective>0) {
			return Result.ok();
		}
		return Result.error("更新失败！");
	}
	/**
	 * 删除某一个商品类别
	 * @param token
	 * @param goodsCategory
	 * @return
	 */
	@Override
	public Result deleteGoodsCategory(String token, MineGoodsCategory goodsCategory) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		//先查询该用户是否已经存在了店铺
		if (user.getShopId()==null) {
			return Result.error("未创建店铺，添加失败！");
		}
		int deleteByPrimaryKey = mineGoodsCategoryMapper.deleteByPrimaryKey(goodsCategory.getId());
		if (deleteByPrimaryKey>0) {
			return Result.ok();
		}
		return Result.error("更新失败！");
	}
	/**
	 * 下架某一个商品
	 * @param token
	 * @param goods
	 * @return
	 */
	@Override
	public Result updateGoodsUnshelve(String token, MineGoods goods) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		//先查询该用户是否已经存在了店铺
		if (user.getShopId()==null) {
			return Result.error("未创建店铺，修改失败！");
		}
		//商品状态，1-正常，2-下架，3-删除
		goods.setStatus((byte) 2);
		goods.setUpdateTime(new Date());
		int keySelective = goodsMapper.updateByPrimaryKeySelective(goods);
		if (keySelective>0) {
			return Result.ok();
		}
		return Result.error("下架失败！");
	}
	/**
	 * 获取商铺所有商品列表 分页展示
	 */
	@Override
	public Result getGoodsList(String token, int page, int rows) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		//先查询该用户是否已经存在了店铺
		if (user.getShopId()==null) {
			return Result.error("未创建店铺！");
		}
		
		//过滤已经下降和已经删除的商品
		MineGoods goods = new MineGoods();
		goods.setStatus((byte) 1);
		goods.setUserId(user.getId());
		PageHelper.startPage(page, rows);
		List<MineGoods> goodsList = gMapper.findGoodsList(goods);
		
		PageInfo<MineGoods> pageInfo = new PageInfo<MineGoods>(goodsList);

		return Result.success(pageInfo);
		
		//MineGoodsExample example =new MineGoodsExample();
		
//		Criteria criteria = example.createCriteria();
//		criteria.andStatusEqualTo((byte) 1);
//		criteria.andUserIdEqualTo(user.getId());
//		PageHelper.startPage(page, rows);
//		List<MineGoods> list = goodsMapper.selectByExample(example);
//		
//		PageInfo<MineGoods> pageInfo = new PageInfo<MineGoods>(list);
//
//		return Result.success(pageInfo);
	}
	@Override
	public Result getGoodsByCategoryId(GoodsQueryVo queryVo) {
		
		//查询数据列表和数据总数
		List<GoodsDataInfo> findGoodsByCategoryId = gMapper.findGoodsByCategoryId(queryVo);
		Integer count = gMapper.findGoodsByCategoryIdCount(queryVo);
		Page<GoodsDataInfo> page = new Page<GoodsDataInfo>();
		page.setTotal(count);                //数据总数
		page.setSize(queryVo.getSize());     //每页显示条数
		page.setPage(queryVo.getPage());     //当前页数
		page.setRows(findGoodsByCategoryId); //数据列表

		return Result.success(page);
	}
	@Override
	public Result getGoodsAll(GoodsQueryVo goodsQueryVo) {
		
		//查询数据列表和数据总数
		List<GoodsDataInfo> findGoodsByCategoryId = gMapper.findGoodsAll(goodsQueryVo);
		Integer count = gMapper.findGoodsAllCount(goodsQueryVo);
		Page<GoodsDataInfo> page = new Page<GoodsDataInfo>();
		page.setTotal(count);                //数据总数
		page.setSize(goodsQueryVo.getSize());     //每页显示条数
		page.setPage(goodsQueryVo.getPage());     //当前页数
		page.setRows(findGoodsByCategoryId); //数据列表

		return Result.success(page);
	}
	@Override
	public Result getGoodsAllCategory(Long shopId) {
		MineGoodsCategoryExample example = new MineGoodsCategoryExample();
		com.mine.pojo.MineGoodsCategoryExample.Criteria criteria = example.createCriteria();
		criteria.andShopIdEqualTo(shopId);
		criteria.andStatusEqualTo((byte) 1);
		List<MineGoodsCategory> selectByExample = mineGoodsCategoryMapper.selectByExample(example);
		
		return Result.success(selectByExample);
	}
	@Override
	public Result getGoodsDetail(Long goodsId) {
		GoodsDataInfo goodsDetailById = gMapper.getGoodsDetailById(goodsId);
		
		return Result.success(goodsDetailById);
	}
	@Override
	public Result getManagerAllCategory(String token) {
		//先查询用户信息
		MineUser user = userService.getRedisUserByToken(token);
		if (user==null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		//先查询该用户是否已经存在了店铺
		if (user.getShopId()==null) {
			return Result.error("未创建店铺，修改失败！");
		}
		MineGoodsCategoryExample example = new MineGoodsCategoryExample();
		com.mine.pojo.MineGoodsCategoryExample.Criteria criteria = example.createCriteria();
		criteria.andShopIdEqualTo(user.getShopId());
		//criteria.andStatusEqualTo((byte) 1);
		List<MineGoodsCategory> selectByExample = mineGoodsCategoryMapper.selectByExample(example);
		
		return Result.success(selectByExample);
	}

}
